﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;

public partial class Administrator_ManagerWebSite : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void grdManagerWeb_DataBinding(object sender, EventArgs e)
    {

    }

    protected void grdManagerWeb_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
    }

    
    protected void  grdManagerWeb_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {

    }

    [WebMethod, System.Web.Script.Services.ScriptMethod]
    public static JsonEstore GetEstoreByID(string key)
    {
        JsonEstore data = new JsonEstore();
        try
        {
            var repo = new Repository();
            var es  = repo.GetEstoreByID(key);
            data.Name = es.EStore_Name;
            data.ID = es.EStore_Id;
            data.KindToDate = es.EStore_KindToDate;
        }
        catch(Exception ex)
        {
            throw ex;
        }
        return data;

    }

    [WebMethod]
    public static bool BusinessUpgrade(string id, string kindId)
    {
        try
        {
            using (Up1VNDataContext db = new Up1VNDataContext())
            {
                EStore es = db.EStores.Single(s => s.EStore_Id == Int32.Parse(id));
                PointBackage pb = db.PointBackages.Single(p=>p.PointBackage_Id == Int32.Parse(kindId));
                int? dayNo = pb.PointBackage_DayNo;
                DateTime today = DateTime.Now;
                DateTime toDate = today.AddDays((double)dayNo);
                int? pointRemain = es.EStore_TotalPoint - pb.PointBackage_Point;
                if (pointRemain >= 0)
                {
                    SaveUpgrade(id, kindId, today, toDate, pointRemain);
                    return true;
                }
                else
                {
                    return false;
                }
                
            }
        }
        catch (Exception ex)
        {
            throw ex;         
        }
    }

    public static void SaveUpgrade(string id, string kindId, DateTime fromDate, DateTime toDate,int? totalPoint)
    {
        try
        {
            using (Up1VNDataContext db = new Up1VNDataContext())
            {
                EStore es = db.EStores.Single(s => s.EStore_Id == Int32.Parse(id));
                es.EStore_KindId = Int32.Parse(kindId);
                es.EStore_KindFromDate = fromDate;
                es.EStore_KindToDate = toDate;
                es.EStore_TotalPoint = totalPoint;
                db.SubmitChanges();
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}